<?php

namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PluginCreate extends Command
{
    protected static $defaultName = 'plugin:create';
    protected static $defaultDescription = '创建RC框架插件';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::REQUIRED, 'App plugin name');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $output->writeln("Create App Plugin $name");

        if (strpos($name, '/') !== false) {
            $output->writeln('<error>Bad name, name must not contain character \'/\'</error>');
            return self::FAILURE;
        }

        // Create dir config/plugin/$name
        if (is_dir($plugin_config_path = base_path() . "/plugin/$name")) {
            $output->writeln("<error>Dir $plugin_config_path already exists</error>");
            return self::FAILURE;
        }

        $this->createAll($name);

        return self::SUCCESS;
    }

    /**
     * @param $name
     * @return void
     */
    protected function createAll($name)
    {
        $base_path = base_path();
        $this->mkdir("$base_path/plugin/$name/app/controller/admin", 0777, true);
        $this->mkdir("$base_path/plugin/$name/app/middleware", 0777, true);
        $this->mkdir("$base_path/plugin/$name/config", 0777, true);
        $this->createFunctionsFile("$base_path/plugin/$name/app/functions.php");
        $this->createControllerFile("$base_path/plugin/$name/app/controller/admin/Index.php", $name);
        $this->createConfigFiles("$base_path/plugin/$name/config", $name);
    }

    /**
     * @param $path
     * @return void
     */
    protected function mkdir($path)
    {
        if (is_dir($path)) {
            return;
        }
        echo "Create $path\r\n";
        mkdir($path, 0777, true);
    }


    /**
     * @param $path
     * @param $name
     * @return void
     */
    protected function createControllerFile($path, $name)
    {
        $content = <<<EOF
<?php

namespace plugin\\$name\\app\\controller\\admin;

use core\controller\AdminBase;
use support\\Request;

class Index extends AdminBase
{
    public function index()
    {
        
    }

}

EOF;
        file_put_contents($path, $content);
    }


    /**
     * @param $base
     * @param $name
     * @return void
     */
    protected function createConfigFiles($base, $name)
    {

        // autoload.php
        $content = <<<EOF
                <?php
                return [
                    'files' => [
                        base_path() . '/plugin/$name/app/functions.php',
                    ]
                ];
                EOF;
        file_put_contents("$base/autoload.php", $content);


        // log.php
        $content = <<<EOF
                    <?php

                    return [
                        'default' => [
                            'handlers' => [
                                [
                                    'class' => Monolog\\Handler\\RotatingFileHandler::class,
                                    'constructor' => [
                                        runtime_path() . '/logs/$name.log',
                                        7,
                                        Monolog\\Logger::DEBUG,
                                    ],
                                    'formatter' => [
                                        'class' => Monolog\\Formatter\\LineFormatter::class,
                                        'constructor' => [null, 'Y-m-d H:i:s', true],
                                    ],
                                ]
                            ],
                        ],
                    ];

                    EOF;


        file_put_contents("$base/log.php", $content);



        //execption
        file_put_contents("$base/exception.php", "<?php return include config_path() . '/exception.php';");

        //middleware
        file_put_contents("$base/middleware.php", "<?php return include config_path() . '/middleware.php';");

        //view
        file_put_contents("$base/view.php", "<?php return include config_path() . '/view.php';");
        
        $this->createAppFile("$base/app.php");
    }


    /**
     * @param $file
     * @return void
     */
    protected function createFunctionsFile($file)
    {
        $content = <<<EOF
<?php
/**
 * Here is your custom functions.
 */



EOF;
        file_put_contents($file, $content);
    }


    protected function createAppFile($file)
    {
        $content = <<<EOF
        <?php

        use support\Request;
        return [
            'debug' => true,
            'controller_suffix' => '',
            'controller_reuse' => false,
            'version' => '1.0.0'
        ];

        EOF;
        file_put_contents($file, $content);
    }
}
